using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace TripleMatch
{
    public class MovableElement : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    { 
        public bool movable = true;

        private void Start()
        {
            //通过长按的方式激活移动（打算先做遮罩）
            //EventTriggerListener.Get(gameObject).onDown += OnPressDown;
            //EventTriggerListener.Get(gameObject).onUp += OnPressUp;
        }

        private RectTransform _rectTransform;
        private RectTransform rectTransform
        {
            get
            {
                if (_rectTransform == null)
                {
                    _rectTransform = GetComponent<RectTransform>();
                }
                return _rectTransform;
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (!movable) return;
            m_isMoved = true;
            if (onBeginDrag != null) onBeginDrag();
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (!movable) return;
            var delta = eventData.delta;
            rectTransform.anchoredPosition += delta;
            if (onDrag != null) onDrag();
        }


        public void OnEndDrag(PointerEventData eventData)
        {
            if (!movable) return;
            m_isMoved = false;
            if (onEndDrag != null) onEndDrag();
        }

        private bool m_isMoved;

        public event Action onBeginDrag;

        public event Action onDrag;

        public event Action onEndDrag;
    }
}

